package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommoditySpecificationCategoryRelationMapper;
import com.ztgf.commodity.models.bo.CommoditySpecificationCategoryRelationSaveBO;
import com.ztgf.commodity.models.dto.CommoditySpecificationCategoryRelationDTO;
import com.ztgf.commodity.models.service.ICommoditySpecificationCategoryRelationService;
import com.ztgf.commodity.models.service.ICommoditySpecificationService;
import com.ztgf.commodity.models.vo.CommodityAttrVO;
import com.ztgf.commodity.models.vo.SpecificationCategoryVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 分类规格关系表控制器
 *
 * @author chenyu.wang
 * @Date 2020-05-27 11:05:46
 */
@DubboService
public class CommoditySpecificationCategoryRelationServiceImpl extends ServiceImpl<CommoditySpecificationCategoryRelationMapper, CommoditySpecificationCategoryRelationDTO> implements ICommoditySpecificationCategoryRelationService {

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private ICommoditySpecificationService specificationService;

    @Override
    public boolean add(CommoditySpecificationCategoryRelationSaveBO saveBO) {
        CommoditySpecificationCategoryRelationDTO dto = new CommoditySpecificationCategoryRelationDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<SpecificationCategoryVO> getSpecificationCategory(Long id, Long brandId,String filterName) {
        List<SpecificationCategoryVO> list = baseMapper.getSpecificationCategory(id, brandId,filterName);
        if (list != null && list.size() > 0) {
            for (SpecificationCategoryVO specificationCategory : list) {
                List<CommodityAttrVO> attrList = baseMapper.getAttrList(specificationCategory.getSpecificationId());
                specificationCategory.setAttrList(attrList);
            }
        }
        return list;
    }

    @Override
    public void addList(Long userId, Long categoryId, String ids) {
        CommoditySpecificationCategoryRelationDTO dto;
        List<CommoditySpecificationCategoryRelationDTO> insertList = new ArrayList<>();
        String[] id = ids.split(",");
        List<String> list = Arrays.asList(id);
        if (list.size() > 0) {
            for (String s : list) {
                dto = new CommoditySpecificationCategoryRelationDTO();
                dto.setId(ymlConfig.getPkId());
                dto.setCreateUserId(userId);
                dto.setCategoryId(categoryId);
                dto.setSpecificationId(s);
                insertList.add(dto);
            }
            this.insertBatch(insertList);
        }
    }

    @Override
    public boolean deleteAccordingToCategoryId(Long categoryId) {
        Wrapper<CommoditySpecificationCategoryRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        return this.delete(wrapper);
    }
}
